package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.mapper.UserMapper;
import com.campus.mapper.UserRoleMapper;
import com.campus.model.TSysUser;
import com.campus.model.TSysUserRole;
import com.campus.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, TSysUserRole>
        implements IUserRoleService {


    @Autowired
    private UserRoleMapper roleMapper;

    @Autowired
    private UserMapper userMapper;

    public TSysUserRole select(String userName, String password){
        QueryWrapper<TSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name",userName).eq("password",password);
        TSysUser sysUser = userMapper.selectOne(queryWrapper);

        QueryWrapper<TSysUserRole> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id",sysUser.getId());
        return roleMapper.selectOne(queryWrapper1);
    }


    @Override
    public String getRole(Long roleId) {
        if (roleId == 1) {
            return "admin";
        } else if (roleId == 2) {
            return "teacher";
        } else if (roleId == 3) {
            return "student";
        } else {
            return "unknown";
        }
    }

    @Override
    public String getRoleByUserId(Long userId) {
        QueryWrapper<TSysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        TSysUserRole role = roleMapper.selectOne(queryWrapper);
        return getRole(role.getRoleId());
    }

    @Override
    // false 不存在  true 存在
    public Boolean isExist(Long userId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        if (roleMapper.selectOne(qw) == null) {
            return false;
        } else {
            return true;
        }
    }


}
